package com.xiaoma.common.ui

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.xiaoma.common.extend.bind

/**
 * author: liangjingbo
 * date: 2022/6/16
 * describe:
 */
abstract class BaseFragment<VB : ViewBinding>(
    inflate: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : Fragment() {

    protected var TAG = "${this.javaClass.simpleName}_XMTag"
    val binding by bind(inflate)

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.i(TAG, "onAttach() hasCode: ${hashCode()}")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i(TAG, "onCreate() hasCode: ${hashCode()}")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.i(TAG, "onCreateView() hasCode: ${hashCode()}")
        binding.root.isClickable = true
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.i(TAG, "onViewCreated() hasCode: ${hashCode()}")
        initView(savedInstanceState)
        initEvent(savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
        Log.i(TAG, "onStart() hasCode: ${hashCode()}")
    }

    override fun onResume() {
        super.onResume()
        Log.i(TAG, "onResume() hasCode: ${hashCode()}")
    }

    override fun onPause() {
        super.onPause()
        Log.i(TAG, "onPause() hasCode: ${hashCode()}")
    }

    override fun onStop() {
        super.onStop()
        Log.i(TAG, "onStop() hasCode: ${hashCode()}")
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        Log.i(TAG, "onHiddenChanged() hidden: $hidden hasCode: ${hashCode()}")
    }

     override fun onDestroyView() {
        super.onDestroyView()
        Log.i(TAG, "onDestroyView() hasCode: ${hashCode()}")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(TAG, "onDestroy() hasCode: ${hashCode()}")
    }

    override fun onDetach() {
        super.onDetach()
        Log.i(TAG, "onDetach() hasCode: ${hashCode()}")
    }

    abstract fun initView(savedInstanceState: Bundle?)

    abstract fun initEvent(savedInstanceState: Bundle?)

    open fun onDestroyViewProxy() {
        Log.i(TAG, "onDestroyViewProxy() ")
    }


    open fun onBackPressed(): Boolean {
        Log.i(TAG, "onBackPressed() ")
        return false
    }

}


fun Fragment.getColorRes(@ColorRes id: Int) = ContextCompat.getColor(requireContext(), id)

fun Fragment.getDrawableRes(@DrawableRes id: Int) = ContextCompat.getDrawable(requireContext(), id)